

/**
 * 两个数组的全集（不重复）
 * @param arr1 
 * @param arr2 
 * @param compare 
 */
export const union = <T>(arr1: T[], arr2: T[], compare: (a: T, b: T) => boolean = (a, b) => a === b) => {
    const arr = [...arr1, ...arr2]
    return arr.filter((item, index) => {
        return arr.findIndex((item2) => compare(item, item2)) === index
    })
}

/**
 * 两个数组的交集（不重复）
 * @param arr1 数组1
 * @param arr2 
 * @param compare 
 * @returns 
 */
export const diff = <T>(arr1: T[], arr2: T[], compare: (a: T, b: T) => boolean = (a, b) => a === b) => {
    return arr1.filter((item) => {
        return !arr2.some((item2) => compare(item, item2))
    })
}
/**
 * 获取数组2不在数组1中的元素
 * @param arr1 数组1
 * @param arr2 数组2
 * @param compare 
 * @returns 数组2不在数组1中的元素
 */
export const diff2 = <T>(arr1: T[], arr2: T[], compare: (a: T, b: T) => boolean = (a, b) => a === b) => {
    return arr2.filter((item) => {
        return !arr1.some((item2) => compare(item, item2))
    })
}
